package java10;
import java.sql.*;
public class JdbcTest01 {
    public static void main(String[] args){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //通过DriverManager获取数据库连接
//            String url = "jdbc:mysql://localhost:3306/jdbc";
            String url = "jdbc:mysql://localhost:3306/mybatis";
            String username = "root";
            String password = "root";
            conn = DriverManager.getConnection(url,username,password);
            //通过Connection对象获取Statement对象
            stmt = conn.createStatement();
            //使用Statement执行SQL语句
            String sql = "select * from user";
            rs = stmt.executeQuery(sql);
            //操作ResultSet结果集
//            System.out.println("id  |   name    |   sex " + "   |   email   |   birthday");
            while(rs.next()){
                int id = rs.getInt("id");//通过列名获取指定字段的值
                String name = rs.getString("name");
                String pwd = rs.getString("pwd");
//                String sex = rs.getString("sex");
//                String email = rs.getString("email");
//                Date birthday = rs.getDate("birthday");
//                System.out.println(id + "   |   " + name + "    |   " + sex + "    |    " + email + "   |   " + birthday);
//                System.out.println(id + name + pwd);

                String str = name;
                char [] chars =str.toCharArray();//获取原始编码
                System.out.println(chars);
                byte [] bytes = new byte[chars.length];
                for (int i = 0; i < bytes.length; i++) {
                    bytes[i] = (byte)chars[i];
//                    System.out.println(bytes[i]);
                }
                System.out.println("转换后:" + new String(bytes));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            //关闭连接，释放资源
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
